﻿using RemoteManger.Entity;
using RemoteManger.Remote;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RemoteManger.Remote
{
 
    public partial class RemoteList : RemoteMain
    {
        private List<MachineView> machines = new List<MachineView>();
        private List<MachineGroupInfo> machineGroups = new List<MachineGroupInfo>();
        public RemoteList()
        {

            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
            InitializeData();
            base.Load += new EventHandler(Form1_Load);
            base.SizeChanged += new EventHandler(Form1_SizeChanged);
        }

        private void InitializeData()
        {
            new List<MachineView>();
            machineGroups = db.Queryable<MachineGroupInfo>().ToList();
            List<MachineInfo> list = db.Queryable<MachineInfo>().ToList();
            machines.Clear();
            foreach (MachineInfo item in list)
            {
                MachineView machineView = item.ToJson().ToObject<MachineView>();
                machineView.Connect = "连接";
                machines.Add(machineView);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            panel1.AutoScroll = true;
            UpdateLayout();
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            UpdateLayout();
        }

        private void OpenRemoteDesk_Click(object? sender, EventArgs e)
        {
            panel1.Controls.OfType<GroupBox>().ToList().ForEach(delegate (GroupBox p)
            {
                p.BackColor = Color.White;
            });
            MachineView machineView = (MachineView)((Button)sender).Tag;
            new Remote(machineView.Ip, machineView.UserName, machineView.Port, machineView.Password).Show();
        }

        private void StudentButton_AddClick(object sender, EventArgs e)
        {
            new AddLink(((Button)sender).Tag.ToLong(), RefushData).Show();
        }

        public override void RefushData()
        {
            InitializeData();
            UpdateLayout();
        }

        private void UpdateLayout()
        {
            panel1.Controls.Clear();
            int num = 10;
            int num2 = 0;
            int num3 = 180;
            int num4 = 10;
            _ = (base.ClientSize.Width - 20) / (num3 + num4);
            panel1.Width = base.ClientSize.Width - 20;
            foreach (MachineGroupInfo item in machineGroups)
            {
                GroupBox groupBox = new GroupBox();
                groupBox.Text = item.GroupName + "  " + item.Remark;
                groupBox.Location = new Point(10, num);
                groupBox.Size = new Size(panel1.Width, 0);
                groupBox.Click += new EventHandler(GroupBoxClick);
                int num5 = 10;
                int num6 = 30;
                foreach (MachineView item2 in machines.Where((MachineView p) => p.GroupId == item.Id).ToList())
                {
                    Button button = new Button();
                    button.MouseDoubleClick += new MouseEventHandler(OpenRemoteDesk_Click);
                    DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(14, 3);
                    defaultInterpolatedStringHandler.AppendLiteral("名称：");
                    defaultInterpolatedStringHandler.AppendFormatted(item2.Title);
                    defaultInterpolatedStringHandler.AppendLiteral("\r\nIp:");
                    defaultInterpolatedStringHandler.AppendFormatted(item2.Ip);
                    defaultInterpolatedStringHandler.AppendLiteral("\r\n标签: ");
                    defaultInterpolatedStringHandler.AppendFormatted(item2.Remark);
                    button.Text = defaultInterpolatedStringHandler.ToStringAndClear();
                    button.TextAlign = ContentAlignment.MiddleLeft;
                    button.Location = new Point(num5, num6);
                    button.Size = new Size(num3, 100);
                    button.Tag = item2;
                    button.Name = "Remote_" + item2.Id;
                    groupBox.Controls.Add(button);
                    num5 += num3 + num4;
                    if (num5 + num3 + num4 > panel1.Width)
                    {
                        num5 = 10;
                        num6 += 120;
                    }

                }
                Button button2 = new Button();
                button2.Text = "+";
                button2.Location = new Point(num5, num6);
                button2.Size = new Size(num3, 100);
                button2.Tag = item.Id;
                button2.Click += new EventHandler(StudentButton_AddClick);

                groupBox.Controls.Add(button2);
                groupBox.Size = new Size(panel1.Width, num6 + 120);
                panel1.Controls.Add(groupBox);
                num += num6 + 120;
                num2 += num6 + 120;
            }
            panel1.Height = base.ClientSize.Height - 30;
        }

        private void GroupBoxClick(object? sender, EventArgs e)
        {
            Control control = (GroupBox)sender;
            if (!(control is GroupBox))
            {
                return;
            }
            if (control.BackColor == Color.Gray)
            {
                control.BackColor = Color.White;
                return;
            }
            panel1.Controls.OfType<GroupBox>().ToList().ForEach(delegate (GroupBox p)
            {
                p.BackColor = Color.White;
            });
            control.BackColor = Color.Gray;
        }

        protected override void OnResizeEnd(EventArgs e)
        {
            base.OnResizeEnd(e);
            UpdateLayout();
        }



        //private void InitializeComponent()
        //{
        //    panel1 = new System.Windows.Forms.Panel();
        //    SuspendLayout();
        //    panel1.Location = new System.Drawing.Point(12, 66);
        //    panel1.Name = "panel1";
        //    panel1.Size = new System.Drawing.Size(934, 384);
        //    panel1.TabIndex = 6;
        //    base.AutoScaleDimensions = new System.Drawing.SizeF(9f, 20f);
        //    base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        //    base.ClientSize = new System.Drawing.Size(958, 492);
        //    base.Controls.Add(panel1);
        //    base.Name = "Form1";
        //    Text = "远程管理工具";
        //    base.Controls.SetChildIndex(panel1, 0);
        //    ResumeLayout(false);
        //    PerformLayout();
        //}
    }
}
